package Html;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;

public class MyTest{

	public static String NAME = "";
	public static String URL = "http://www.tianya.cn/publicforum/content/funinfo/1/1931288.shtml";
	public static int FROM = 151;
	public static int TO = 172;
	public static String DIR = "d:/louzhu.txt";
	
	public static void main(String[] args) {
		AlertieHttpResponse alertieHttpResponse = new AlertieHttpResponse();
		HttpClientUtil httpClientUtil = HttpClientUtil.getInstance();
		NameValuePair[] pairs = {}; 
		HttpClient client = new HttpClient();
		try {
			alertieHttpResponse = httpClientUtil.postHttpResponse(URL, pairs, client);
		} catch (NetworkException e) {
			e.printStackTrace();
		}
		String P_ = "<input type='hidden' name='idArticleslist' value='";
		String _P = ",'>";
		String pages = "";
		if(alertieHttpResponse.getStatuscode() == HttpStatus.SC_OK){
			try {
				pages = new String(alertieHttpResponse.getResponse(),"GBK");
				pages = pages.substring(pages.indexOf(P_)+P_.length(), pages.indexOf(_P, pages.indexOf(P_)));
			}catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		String[] p = pages.split(",");
		int from = FROM -1;
		int to = TO == 0 ? p.length : TO;
		String url_ = URL.substring(0,URL.lastIndexOf("/")+1);
		String _url = URL.substring(URL.lastIndexOf("."),URL.length());
		String fileConetnt = "";
		for(int i = from; i < to; i++){
			String url = url_ + p[i] + _url;
			System.out.println(url);
			try {
				alertieHttpResponse = httpClientUtil.postHttpResponse(url, pairs, client);
			} catch (NetworkException e) {
				e.printStackTrace();
			}
			if(alertieHttpResponse.getStatuscode() == HttpStatus.SC_OK){
				try {
					String html = new String(alertieHttpResponse.getResponse(),"GBK");
					html = html.replaceAll("\r|\n", "");
					String n = "target=_blank>"+NAME+"</a>";
					String d = "提交日期：";
					String c = "</TD></TR></table>";
					String e = "<br><TABLE cellspacing";
					String louzhu = getContent(html, n, d, c, e);
					fileConetnt += louzhu;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
					continue;
				}
			}
		}
		try{
			Writer out = new OutputStreamWriter(new FileOutputStream(DIR),"GBK");
			out.write(fileConetnt);
			out.close();
			System.out.println("---------end--------");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static String getContent(String html,String n,String d,String c,String e){
		String res = "";
		if(html.indexOf(n) != -1){
			html = html.substring(html.indexOf(n));
			int dateindex = html.indexOf(d, html.indexOf(n)) + d.length();
			String date = html.substring(dateindex,dateindex+19);
			int contentindex = html.indexOf(c, html.indexOf(d)) + c.length();
			html = html.substring(contentindex);
			if(html.indexOf(e) != -1){
				String content = html.substring(0, html.indexOf(e));
				content = content.replaceAll("<br>", "\n");
				res += date + "\n" + content + "\n";
				d = "回复日期：";
				res += getContent(html, n, d, c, e);
			}
		}
		return res;
	}
}
